// 路由: 用户接口
const { login } = require('../controller/user');
const { SuccessModel, ErrorModel } = require('../model/resModel')

const handleUserRouter = (req, res) => {
  const method = req.method;
  const path = req.path;

  if (method === 'POST' && path === '/api/user/login') {
    const { username, password } = req.body;
    const result = login(username, password);
    return result.then(data => {
      if (data.username) {
        return new SuccessModel()
      } else {
        return new ErrorModel('登录失败')
      }
    })
  }

  if (method === 'GET' && path === '/api/user/login-test') {
    if (req.cookies.username) {
      return Promise.resolve(
        new SuccessModel()
      )
    }
    return Promise.resolve(new ErrorModel('尚未登录'))
  }
}


module.exports = handleUserRouter
